package com.laima.basic.service.impl;

import com.laima.basic.Exception.BusinessException;
import com.laima.basic.dao.SysDictDataMapper;
import com.laima.basic.model.SysDictData;
import com.laima.basic.service.SysDictDataService;
import com.laima.basic.utils.DictUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: backend-server
 * @description: 数据字典实现层
 * @author: T
 * @create: 2021-03-30 10:21
 **/
@Service
public class SysDictDataServiceImpl implements SysDictDataService {

    @Autowired
    private SysDictDataMapper sysDictDataMapper;


    //此处做分页
    @Override
    public List<SysDictData> selectDictDataList(SysDictData dictData) {
        return sysDictDataMapper.selectDictDataList(dictData);
    }

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType 字典类型
     * @return
     */
    @Override
    public List<SysDictData> selectDictDataByType(String dictType) {
        return sysDictDataMapper.selectDictDataByType(dictType);
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType  字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue) {
        return sysDictDataMapper.selectDictLabel(dictType, dictValue);
    }

    /**
     * 根据字典数据ID查询信息
     *
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public SysDictData selectDictDataById(Long dictCode) {
        return sysDictDataMapper.selectDictDataById(dictCode);
    }



    /**
     * 查询字典数据
     *
     * @param dictType 字典类型
     * @return 字典数据
     */
    @Override
    public int countDictDataByType(String dictType) {
        return sysDictDataMapper.countDictDataByType(dictType);
    }

    /**
     * 通过字典ID删除字典数据信息
     *
     * @param dictCode 字典数据ID
     * @return 结果
     */
    @Override
    public int deleteDictDataById(Long dictCode) {
        //删除时并清空缓存
        int row = sysDictDataMapper.deleteDictDataById(dictCode);
        if (row > 0) {
            //清缓存
            DictUtils.clearDictCache();
        }
        return row;
    }

    /**
     * 批量删除字典数据信息
     *
     * @param dictCodes 需要删除的字典数据ID
     * @return 结果
     */
    @Override
    public int deleteDictDataByIds(Long[] dictCodes) {
        int row = sysDictDataMapper.deleteDictDataByIds(dictCodes);
        if (row > 0) {
            //清缓存
            DictUtils.clearDictCache();
        }
        return row;
    }

    /**
     * 新增字典数据信息
     *
     * @param dictCode 字典数据信息
     * @return 结果
     */
    @Override
    public int insertDictData(SysDictData dictCode) {
        //先查询是否存在
         SysDictData sysDictData= sysDictDataMapper.selectById(dictCode);
        if (sysDictData != null) {
            throw new BusinessException("数据已存在,请修改···");
        }
        int row = sysDictDataMapper.insertDictData(dictCode);
        if (row > 0) {
            //添加清缓存
            DictUtils.clearDictCache();
        }
        return row;
    }

    /**
     * 修改字典数据信息
     *
     * @param dictData 字典数据信息
     * @return 结果
     */
    @Override
    public int updateDictData(SysDictData dictData) {
        int row = sysDictDataMapper.updateDictData(dictData);
        if (row > 0) {
            //添加清缓存
            DictUtils.clearDictCache();
        }
        return row;
    }
}
